package com.northpool.operator.cicd.notice;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.utils.StpOperatorHelper;

@OperatorInfo(
        ref = "WeChatBootOperator",
        name = "企业微信通知",
        description = "企业微信发送图文通知",
        version = "news"
)
public class WeChatBotNewsOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "webhook", name = "WebHook地址", desc = "WebHook地址, 如: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1abb0828-9c74-4e30-a1b0-cfa17c82512a", required = true),
            @InputParam(ref = "title", name = "标题", desc = "标题，不超过128个字节，超过会自动截断", required = true),
            @InputParam(ref = "description", name = "描述", desc = "描述，不超过512个字节，超过会自动截断", value = ""),
            @InputParam(ref = "url", name = "跳转连接", desc = "点击后跳转的链接。", required = true),
            @InputParam(ref = "picurl", name = "图片链接", desc = "图文消息的图片链接，支持JPG、PNG格式，较好的效果为大图 1068*455，小图150*150。", value = "")
    })
    public WeChatBotNewsOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(WeChatBotNewsOperator.class, TestParamBuilder.aBuilder()
                .param("webhook", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1abb0828-9c74-4e30-a1b0-cfa17c82512a")
                .param("title", "标题测试")
                .param("description", "描述测试")
                .param("url", "http://northpool.work:5244")
                .param("picurl", "http://northpool.work:9000/stp-minio/images/node_default.png")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String webhook = paramTool.getStr("webhook");
        String title = paramTool.getStr("title");
        String description = paramTool.getStr("description");
        String url = paramTool.getStr("url");
        String picurl = paramTool.getStr("picurl");

        String format = StrUtil.format("{\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\"{}\",\"description\":\"{}\",\"url\":\"{}\",\"picurl\":\"{}\"}]}}"
                , title, description, url, picurl);
        HttpResponse response = HttpUtil.createPost(webhook)
                .body(format)
                .timeout(10000)
                .execute();
        if (response.isOk()) {
            JSONObject resObj = JSONObject.parseObject(response.body());
            String errmsg = resObj.getString("errmsg");
            if ("ok".equals(errmsg)) {
                this.log("发送成功");
            } else {
                this.log("发送失败, 响应体: {}", response.body());
            }
        } else {
            this.log("发送失败, 响应状态码: {}, 响应体: {}", response.getStatus(), response.body());
        }
    }
}
